<?php
/**
 * 毛文科
 */
namespace app\admin\model;
use function GuzzleHttp\Promise\promise_for;
use think\Db;
use think\Exception;
use tp5er\Backup;

class Install{
    protected $config = [];
    protected $sql = [];

    /**安装
     * @param null $modular
     * @return bool
     */
    static public function ins($modular=null){
        $obj = new Install();
        $thismodular = $obj->getModular($modular);
        if(!$thismodular['sql']){
            $thismodular['sql'] = '../application/admin/config/default.sql';
            $bool =  $obj->huanyuan($thismodular['sql'],[
                '`admin_user`'=>config('database.prefix').$modular.'_'.'admin_user',
                '`column`'=>config('database.prefix').$modular.'_'.'column',
                '`databases`'=>config('database.prefix').$modular.'_'.'databases',
                '`database_system`'=>config('database.prefix').$modular.'_'.'database_system',
                '`enclosure`'=>config('database.prefix').$modular.'_'.'enclosure',
                '`fields`'=>config('database.prefix').$modular.'_'.'fields',
                '`jurs`'=>config('database.prefix').$modular.'_'.'jurs',
                '`keywords`'=>config('database.prefix').$modular.'_'.'keywords',
                '`models`'=>config('database.prefix').$modular.'_'.'models',
                '`roles`'=>config('database.prefix').$modular.'_'.'roles',
                '`systems`'=>config('database.prefix').$modular.'_'.'systems',
                '`qiniu`'=>config('database.prefix').$modular.'_'.'qiniu',
                '`mokuais`'=>config('database.prefix').$modular.'_'.'mokuais',
                '`users`'=>config('database.prefix').$modular.'_'.'users',
                '`lange`'=>config('database.prefix').$modular.'_'.'lange',
                '`adminmessage`'=>config('database.prefix').$modular.'_'.'adminmessage',
                '`ziduancid`'=>config('database.prefix').$modular.'_'.'ziduancid',
            ]);
            if($bool){
                $obj->insert($modular);
            }
        }else{
            $bool =  $obj->myhuanyuan($thismodular['sql'],$modular);
//            return $bool;
        }
        if($bool){
            $obj->changeModular($modular);
            return true;
        }else{
            return false;
        }
    }

    /**改变当前模块状态
     * @param $modular
     * @return bool|int
     */
    public function changeModular($modular){
        $array = include '../application/'.$modular.'/config/project.php';
        $array['install'] = 1;
        return MyProject::create($array);
    }

    /**获取当前模块
     * @param $modular
     * @return bool
     */
    public function getModular($modular){
        $array = include '../application/'.$modular.'/config/project.php';
        if($array){
            return $array;
        }
        return false;
    }

    /**设置文件
     * @param $file 文件地址
     * @param array $change 替换的字符串
     * @return $this
     */
    public function setFile($file,$change=[]){
        if($file&&is_file($file)){
            $str = file_get_contents($file);
            foreach ($change as $key=>$value){
                $str = preg_replace($key,$value,$str);
            }
            $this->sql = explode(';',$str);
        }
        return $this;
    }

    /**还原数据
     * @param $file 文件地址
     * @param array $change 替换的字符串
     * @return bool
     */
    public function huanyuan($file,$change=[]){
        $this->setFile($file,$change);
        try{
            foreach ($this->sql as $key=>$value){
                $value = trim($value);
                if($value){
                    Db::execute($value);
                }
            }
            return true;
        }catch (Exception $e){
            return false;
        }
    }
    public function myhuanyuan($file,$project){
        $shuju = explode('/',$file);
        $name = end($shuju);
        $config = [
            'path'=>str_replace($name,'',$file)
        ];
        $db = new Backup($config);
        $db->setFile(['name'=>$file])->import(0);
        $mydb = new MyBeiFen();
        $arrlist = $mydb->getlist($project);
        $shuju = $mydb->addinserts($arrlist,$project);
//        return $shuju;
        MyProject::xcopy('../application/'.$project.'/static/uploads','./uploads/'.$project);
        MyProject::xcopy('../application/'.$project.'/static/static','./static/'.$project);
        MyProject::xcopy('../application/'.$project.'/admin','../application/admin/controller/'.$project);
        return true;
    }
    public function insert($modular){
        $shuju = config('models.');
        foreach ($shuju as $value){
            $value['table'] = $modular.$value['table'];
            Db::name($modular.'_models')->insert($value);
        }
        $field =config('fields.');
        foreach ($field as $value){
            Db::name($modular.'_fields')->insert($value);
        }
        $databasessystem = [
            [
                'id'=>1,
                'name'=>'自动备份',
                'value'=>0
            ],
            [
                'id'=>2,
                'name'=>'自动备份间隔(天)',
                'value'=>0
            ],
            [
                'id'=>3,
                'name'=>'上次备份时间',
                'value'=>0
            ],
        ];
        foreach ($databasessystem as $value){
            Db::name($modular.'_database_system')->insert($value);
        }
        $data = Db::name($modular.'_fields')
            ->where('model_id',15)
            ->select();
        $newdata = [];
        foreach ($data as $value){
            unset($value['id']);
            unset($value['model_id']);
            $value['cid'] = 1;
            $newdata[] = $value;
        }
        Db::name($modular.'_ziduancid')->insertAll($newdata);
        return true;

    }
}